<template>
  <div class="app-container">
    <el-form v-enterToNext="true" ref="postForm" :model="postForm" :rules="rules" status-icon label-width="150px" class="demo-ruleForm">
      <el-form-item label="编号" prop="no">
        <el-input v-model="postForm.no"/>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="postForm.name"/>
      </el-form-item>
      <el-form-item label="安全存量" prop="merchandiseSafetyStock">
        <el-input v-model.number="postForm.merchandiseSafetyStock"/>
      </el-form-item>
      <el-form-item label="单个进价(元)" prop="purchasePrice">
        <el-input v-model.number="postForm.purchasePrice"/>
      </el-form-item>
      <el-form-item label="单个重量(g)" prop="weight">
        <el-input v-model.number="postForm.weight"/>
      </el-form-item>
      <el-form-item label="件/毛重(kg)" prop="packgeWeight">
        <el-input v-model.number="postForm.packgeWeight"/>
      </el-form-item>
      <el-form-item label="装箱数" prop="packgeNum">
        <el-input v-model.number="postForm.packgeNum"/>
      </el-form-item>
      <el-form-item label="整件箱规" prop="packgeCube">
        <el-input v-model="postForm.packgeCube"/>
      </el-form-item>
      <el-form-item label="供应商" prop="supply">
        <el-select v-model="postForm.supply" filterable clearable placeholder="请选择" size="mini">
          <el-option
            v-for="item in supplys"
            :key="item.ID"
            :label="item.Name"
            :value="item.ID"/>
        </el-select>
      </el-form-item>
      <el-form-item label="位置" prop="location">
        <el-autocomplete
          v-model="postForm.location"
          :fetch-suggestions="querySearch"
          class="inline-input"
          placeholder="请输入位置"
          @select="handleSelect"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="postForm.remark"/>
      </el-form-item>
      <el-form-item>
        <el-button :loading="loading" type="primary" size="mini" @click="submitForm">提交</el-button>
        <el-button size="mini" @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

import { createComponent } from '@/api/component'
import { getLocation } from '@/api/commodity'
import { getSupply } from '@/api/supply'

export default {
  data() {
    return {
      loading: false,
      options: [],
      supplys: [],
      locations: [],
      postForm: {
        id: '',
        no: '',
        name: '',
        merchandiseSafetyStock: 0,
        remark: '',
        supply: '',
        location: '',
        packgeNum: 0,
        packgeCube: '',
        packgeWeight: 0,
        weight: 0,
        purchasePrice: 0
      },
      rules: {
        no: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        packgeNum: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        supply: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        location: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        merchandiseSafetyStock: [
          { required: true, message: '请输入', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    submitForm() {
      this.$refs['postForm'].validate(valid => {
        if (valid) {
          this.loading = true
          this.postForm.id = this.postForm.no
          createComponent(this.postForm)
            .then(response => {
              console.log(response)
              this.loading = false
              this.resetForm()
              this.$message.success('创建成功')
            })
            .catch(() => {
              this.loading = false
              this.$message.error('创建失败')
            })
        }
      })
    },
    resetForm() {
      this.postForm.id = ''
      this.postForm.no = ''
      this.postForm.name = ''
      this.postForm.merchandiseSafetyStock = 0
      this.postForm.remark = ''
      this.postForm.supply = ''
      this.postForm.location = ''
      this.postForm.packgeNum = 0
      this.postForm.packgeCube = ''
      this.postForm.packgeWeight = 0
      this.postForm.weight = 0
      this.postForm.purchasePrice = 0
    },
    fetchData() {
      getSupply().then(response => {
        console.log(response)
        this.supplys = response.map(item => {
          return {
            ID: item.Name,
            Name: item.Name
          }
        })
      })
      getLocation().then(response => {
        console.log(response)
        this.locations = response.map(item => {
          return {
            value: item.Location
          }
        })
      })
    },
    querySearch(queryString, cb) {
      var locations = this.locations
      var results = queryString ? locations.filter(this.createFilter(queryString)) : locations
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    createFilter(queryString) {
      return (restaurant) => {
        return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0)
      }
    }
  }
}
</script>
